package com.aplink.generic.widgets;

import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.constant.ConfigParameter;
import com.aplink.generic.constant.SqlDataType;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.GenericObject;
import com.aplink.generic.pojo.ObjPropCol;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.pojo.controlspec.SpinnerSpec;
import com.aplink.generic.util.convertcount.GenericConverter;
import com.aplink.generic.util.convertcount.GenericFilter;
import com.aplink.generic.xmlparser.ObjPropInfoParser;

import java.util.ArrayList;

public class CustomSpinner extends BaseView {
    private Spinner mSpinner;
    private SpinnerSpec mSpinnerSpec;

    public CustomSpinner( GenericFragment dataEntryActivity,
                          ControlSpec controlSpec, View view ) {
        super( dataEntryActivity, controlSpec, view );
        this.setSpinner( (Spinner) view );
        mSpinnerSpec = (SpinnerSpec) getControlSpec();
    }

    @Override
    protected void loadDataToView() {
        // TODO Auto-generated method stub
    }

    @Override
    protected void doIfNotHashPassedValues() {
        // TODO Auto-generated method stub

    }

    protected ArrayList<GenericObject> loadData() {
        ObjPropCol objPropList = ObjPropInfoParser.parseIntoObjProp(
                getApplicationContext(), mSpinnerSpec.getContentDataId(),
                ConfigParameter.OBJ_PROP_TAGNAME, mSpinnerSpec.getDataObject() );
        return getSelectCommand().getList( objPropList, null );
    }

    @Override
    protected void setEventListener() {
        this.mSpinner.setOnItemSelectedListener( getOnItemSelectedListener() );
    }

    @Override
    protected void initViewProperties() {
        final boolean isVisibility = getControlSpec().isVisibility();
        if ( ! isVisibility ) {
            this.mSpinner.setVisibility( View.GONE );
        }
        this.mSpinner.setTag( getControlSpec() );
        SpinnerSpec specFormat = (SpinnerSpec) getControlSpec();
        final ArrayList<GenericObject> genericObjects = this.loadData();
        String[] arrTextValue = new String[genericObjects.size()];
        arrTextValue = GenericFilter.getArrayValueFromAnAttribute(
                genericObjects, specFormat.getDisplayProperty() );
        final ArrayAdapter<String> SpinerAdapter = new ArrayAdapter<String>(
                getApplicationContext(),
                android.R.layout.simple_spinner_dropdown_item, arrTextValue );
        this.mSpinner.setAdapter( SpinerAdapter );

    }

    public Spinner getSpinner() {
        return this.mSpinner;
    }

    public void setSpinner( Spinner spinner ) {
        this.mSpinner = spinner;
    }

    public String getSelectedValue() {
        return mSpinner.getSelectedItem().toString();
    }

    @Override
    public GenericAttribute<?, ?> getAttribute() {
        String name = getControlSpec().getMappingName();
        SqlDataType dataType = GenericFilter
                .getDataTypeOfAttributeFromObjectPropMappingName(
                        getObjPropList(), name );
        String value = mSpinner.getSelectedItem().toString();
        return GenericConverter.getGenericAttribute( name, dataType, value );
    }
}
